﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Micrhard.FMCG.Server.DataAccess;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.DTO;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;

namespace Micrhard.FMCG.Server.Impl
{
    public class DailyApproveImpl : IDailyApprove
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.DailyModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.DailyModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.Dailies.SingleOrDefault(l => l.DailyID == model.DailyID);
            var emp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            if (entity != null)
            {
                db.DailyComments.AddObject(new DailyComment
                {
                    CommentID = Guid.NewGuid(),
                    CreateTime = DateTime.Now,
                    DailyID = entity.DailyID,
                    OperatorID = emp.EmployeeID,
                    Content = model.ApproveAdervise
                });
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, DailyMsg.CommentFailed);
                Log.Error("IDailyApprove.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.DailyModel model)
        {
            throw new NotImplementedException();
        }

        public Models.DailyModel GetModelByID(Guid id)
        {
            DailyModel result = new DailyModel();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var model = db.Dailies.SingleOrDefault(c => c.DailyID == id);
            if (model != null)
            {
                result = model.ToModel();
                //对于非计划的巡店，也在这里显示,获取条件为在日计划时间范围内。
                var start = DateTime.Now.Date;
                var end = start.AddDays(1);
                var UnPlanedProtalSubKA = (from c in db.EmployeeCheckIns
                                           where c.CreateTime >= start && c.CreateTime <= end
                                           select new ProtalSubKAPlanModel
                                           {
                                               CreateTime = start,
                                               Name = c.SubKA.Name,
                                               ProtalSubKAID = Guid.Empty,
                                               ProtalTime = start,
                                               SubKAID = c.SubKAID.Value
                                           }).ToList();
                UnPlanedProtalSubKA.ForEach(i =>
                {
                    bool contains = false;
                    result.TommrowPlanProtalSubKA.ForEach(m =>
                    {
                        if (i.SubKAID == m.SubKAID)
                            contains = true;
                    });
                    if (!contains)
                    {
                        result.TommrowPlanProtalSubKA.Add(i);
                    }
                });
            }
            return result;
        }

        public Models.DailyModel GetModelByModel(Models.DailyModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.DailyModel> Query(Models.DailyModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cmp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            IEnumerable<Daily> entities = null;
            if (cmp.Range == (int)Range.Manager) //经理批阅日报
            {
                entities = from b in db.Dailies
                           where b.Employee.CompanyID == cmp.CompanyID
                           select b;
            }
            else//业务员批阅日报
            {
                var allkas = from s in db.ManagementScopes
                             where s.Employee.UserName == this.scope.CurrentUserName
                             select s.SubKA;
                var salerIDs = from s in db.ManagementScopes
                               from k in allkas
                               where s.SubID == k.SubID && s.Range == (int)Range.Saler
                               select s.EmployeeID;
                entities = from d in db.Dailies
                           from i in salerIDs
                           where d.EmployeeID == i
                           select d;
            }

            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<DailyModel>();
        }

        public List<Models.DailyModel> Query(Models.DailyModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
